import axios from 'axios'
import { Message, MessageBox,Loading } from 'element-ui'
import configUrl from '@/baseconfig.js'
import Cookies from "vue-cookies"
import vue from '@/main.js'

const base_url = process.env.NODE_ENV =='development'?'/api':configUrl.httpsUrl;
/*
* /common 通用接口
* /reckon 需验证接口,参数规整接口
*/
const service = axios.create({
    baseURL: base_url, // api 的 base_url
    timeout: 20000,// 请求超时时间
    headers:{'Content-Type': 'application/json'},
    validateStatus:()=>{
        return true
    }
    
})




const CancelToken = axios.CancelToken
// 设置加载实例
let pendingMessage = null;
let loadingInstance = 0;    // 加载全局的loading
// request拦截器
service.interceptors.request.use(
    config => {
        loadingInstance++;
        return config
    },
    error => {
        // Do something with request error
        loadingInstance--;
        Promise.reject(error)
    }
)
// response 拦截器
service.interceptors.response.use(
    response => {
        loadingInstance--;
        if(response.config.url=="/auth/login"){
            Cookies.get('token',response.headers.token);//使用cookies
        }
        if(response.data.code!==1000){
            Message({
                message: response.data.message,
                type: 'error',
                duration: 5 * 1000
            })
            return Promise.reject(response);
        }else{
            
            const resdata = response.data.data;
            return Promise.resolve(resdata)
        }
    },
    error =>{
        Message({
            message: error.message,
            type: 'error',
            duration: 5 * 1000
        })
        return Promise.reject(error)
    }
)

export default service